Parse-O-Matic: Flow Control

Note: This is an appendix to the "Parse-O-Matic Scripts" user manual.

Table of Contents

You can click on any section title below to jump directly to that section. Basic Flow Control Again Begin Break Call * Continue Done Else End Exit * If Otherwise Procedure * Stop Step Control FileInit and FileDone TaskInit and TaskDone NextStep * This feature requires the Advanced Scripting License

Basic Flow Control

The basic flow control commands, (such as If, Begin, End, Again, Stop), let you control the order in which the lines of your script are executed. You can, for example, execute a block of commands only under certain circumstances, or cause a group of commands to be executed repeatedly ("looping").

Again

Format Again [v1 k2 v3] Examples See the
Begin command Purpose Causes a Begin block to repeat if the comparison is true (or if no comparison is specified) Parameters v1 - Value to be compared k2 - Comparator (click for details) v3 - Value to compare to v1 Restrictions You cannot combine an Again command with an If command.

Begin

Format Begin [v1 k2 v3] Example Begin MyVar = 'XYZ' ; Execute block if MyVar equals 'XYZ' Purpose Marks the start of a conditional block of script code Parameters v1 - Value to be compared k2 -
Comparator (click for details) v3 - Value to compare to v1 Defaults If no comparison is specified, the block always begins. In such case, it makes no sense to have an Else command, and it almost invariably means that the block will end with an Again command. Restrictions You cannot combine a Begin command with an If command. Similar Cmds If Notes Comparisons are not case-sensitive, so 'CAT' = 'Cat' (unless you have altered the CompareCtrl setting). The Begin command does not set the $Success variable! Begin blocks can be nested up to 25 levels deep. Here is an example of the Begin command, used with Else and End: Begin MyVar = 'Cat' OutEnd 'The animal is feline' ; Executed if MyVar = 'Cat' OutEnd 'In fact, it is a cat' ; Executed if MyVar = 'Cat' Else OutEnd 'The animal is not feline' ; Executed if MyVar is not 'Cat' End Note the use of indentation. Indentation of the conditional code blocks is not mandatory, but it does make a complicated script much easier to understand. This is particularly important if a Begin block contains other Begin blocks: Begin CustCode[1 3] = 'USA' OutEnd 'The customer is in the USA' Begin CustCode[4 5] = 'NY' OutEnd 'The customer is in New York' End Begin CustCode[4 5] = 'TX' OutEnd 'The customer is in Texas' End End Without the indentation, the logic of the code above would be hard to follow. Here is an example of the Begin command used in a loop: Counter = 0 Begin Counter = Counter+ OutEnd 'The counter equals ' Counter Again Counter #< 10 This would output the numbers from 1 to 10. You could also do it this way: Counter = 0 Begin Counter #< 10 Counter = Counter+ OutEnd 'The counter equals ' Counter Again This would output the numbers from 1 to 10. If you wish, you can put comparisons on both the Begin and Again. Both tests are repeated on every iteration of the loop.

Break

Format Break Example If CustNum = MaxCustNum Break Purpose Breaks out of the current Begin/Again block, carrying on execution at the line following the next Again command Similar Cmds Continue

Call

Format Call v1 [v2 v3 v4...] Example Call MyProcedure 'Hello!' ; Pass 'Hello!' to MyProcedure Purpose Invoke a generalized section of script code Parameters v1 - The name of the
Procedure; doubles as a variable for passing information to and receiving results back from the Procedure v2 - Value (any number of values can be appended) Defaults If v2 is not specified, the procedure variable v1 is assigned a null value. Restrictions Calls from procedures into other procedures, which in turn call other procedures (and so on), can nest up to 50 levels deep. When you Call a procedure, execution of the script jumps to the first line of the procedure and continues until the corresponding End statement. The name of the procedure is also the variable name containing any parameters passed in v2, v3 and so on (the values are concatenated). Here is a sample script: Call OutWithExclaim 'Hello, ' ' world' ; Call the procedure OutEnd 'Glad you could join us!' ; This line is run after the Call Stop ; Stop running script lines Procedure OutWithExclaim ; Start of the procedure OutWithExclaim = OutWithExclaim '!' ; Add an exclamation point OutEnd OutWithExclaim ; Output End ; Return to the line after the Call This would output the string 'Hello, world!' then return to the line following the Call command.

Continue

Format Continue Example If Status = 'Ignore' Continue Purpose Jumps ahead to the Again of the current Begin/Again block Similar Cmds Break

Done

Format Done Purpose Skips the rest of the script (for the current record) Similar Cmds Stop, NextStep Notes The Done command is usually used with the
If command, or at the end of a Begin/End block.

Else

Format Else Example See the
Begin command Purpose Defines the start of the conditional code block that is executed if the Begin comparison is false. Restrictions You cannot combine an Else command with an If command.

End

Format End Examples See the
Begin command Purpose Marks the end of a Begin block Restrictions You cannot combine an End command with an If command.

Exit

Format Exit Purpose Immediately returns from a Procedure Restrictions The Exit command can only be used inside a
Procedure. Notes The Exit command is typically used in conjunction with a comparison. You do not need to include an Exit command in every Procedure.

If

Format If v1 k2 v3 c4 Examples If CustCode = 'AB12' OutEnd 'Mary Smith' If CustCode = 'CD34' CustAddr = '1234 Happy Lane' Purpose Conditionally performs a command Parameters v1 - Value to be compared k2 -
Comparator (click for details) v3 - Value to compare to v1 c4 - Command Restrictions The If command may not be combined with a command that defines the start of a code block, such as Begin or FileInit. Similar Cmds Begin, End, Again Notes The comparison is case-insensitive, so 'CAT' = 'cat' unless you have altered the CompareCtrl setting. The If command does not set the $Success variable! In deference to the ingrained training of seasoned programmers, you may use the word "then" after the comparison. Thus, the following command will be accepted: If x > y then z = 'Hello' This usage is non-standard, however, and is not recommended. The scripting engine treats the "then" as a variable, but ignores it in this context. Thus, you should never use a variable named "Then". The If command does not have an "Else" option as in most programming languages. To execute a command when the If condition is false, use the Otherwise command. Alternatively, you can use the Begin command with an Else section.

Otherwise

Format Otherwise c1 Example If Animal = 'Cat' Type = 'Feline' ; The initial If command Otherwise Type = 'Non-feline' ; Action taken if false Purpose Executes an alternative command when the If comparison is false Parameters c1 - Command Restrictions The Otherwise command must follow immediately after an If. The Otherwise command may not be combined with a command that defines the start of a code block, such as Begin or FileInit. Similar Cmds Else

Procedure

Format Procedure v1 Example Procedure MyCode Purpose Defines the start of a generalized section of script code, which is terminated with the End command Parameters v1 - The name of the Procedure (must be a simple variable) Restrictions Recursive procedures (i.e. procedures that call themselves) are not formally supported and their use is not recommended. Notes See the
Call command for a full discussion of procedures. As the script is being run, any Procedure sections are ignored when encountered; they are only executed when explicitly invoked by Call. Procedures can go anywhere except within conditional blocks such as Begin/End, FileInit/End and so on. Procedures are usually placed together at the end of the script.

Stop

Format Stop [v1] Example If CustNum[1] = 'X' Stop 'Invalid customer number' Purpose Terminates further processing Parameters v1 - Optional pop-up message Similar Cmds Done, NextStep Notes If v1 is included, a pop-up message is displayed. In such case, the Stop is considered an "abnormal" end of processing and the script-enabled application should proceed accordingly.

Step Control

A simple script runs from top to bottom each time a record is sent to it. But how can you initialize variables before processing starts? How can you output a grand total after all the records have been processed? These issues and others are addressed by the step control commands. When processing files, Parse-O-Matic performs a series of steps: TaskInit Executes before data is read from the first input file FileInit Executes before data is read from the current input file Main Executes once for each record sent to the script FileDone Executes after the last data is read from the current input file TaskDone Executes after the last data is read from the last input file If you are only processing a single file (i.e. you are not using wildcards to process multiple input files), there is little to distinguish TaskInit and TaskDone from FileInit and FileDone. Except for the Main step, each step appears inside a conditional block, as in this example: TaskInit ; Start of the TaskInit step OutEnd 'Customer Count Report' ; Report header OutEnd '---------------------' ; Report header End ; End of the TaskInit step FileInit ; Start of the FileInit step OutEnd 'Input file: ' $ActualIFN ; Output the file name NumInpFiles = NumInpFiles+ ; Count this input file End ; End of the FileInit step CustCount = CustCount+ ; Main step: count record TaskDone ; Start of the TaskDone step OutNull ; Output a blank line OutEnd 'Number of input files: ' NumInpFiles ; Output statistics OutEnd 'Number of customers: ' CustCount ; Output statistics End ; End of the TaskDone step In the example given above, the conditional code for the report header was placed in TaskInit so that the script will output it only once, even if you are processing multiple input files. The conditional steps are optional. For example, you do not have to include FileInit in your script. The conditional steps can appear almost anywhere in your script (though not within another conditional block).

FileInit and FileDone

The FileInit section is executed before each input file is processed. The FileDone section is executed after each input file is processed. For details, click
here. You cannot combine the FileInit or FileDone commands with the If command.

TaskInit and TaskDone

The TaskInit section is executed before data is read from the first input file. The TaskDone section is executed after the last record is read from the last input file. For additional details, click
here. You cannot combine the TaskInit or TaskDone commands with the If command.

NextStep

The NextStep command can be used to jump out of a
step (such as FileInit or Main) and proceed to the next step. For example, if your Main step has already located the information you are seeking, there is no reason to continue reading the input file. In such case, you can execute a NextStep command to ignore the rest of the input file and proceed immediately to FileDone, as in the following example. CustNum = $OutData[1 6] ; Main step: Get customer number PhoneNum = $OutData[60 70] ; Main step: Get the phone number If CustNum = '314159' NextStep ; Main step: Found the customer? FileDone ; Start of the FileDone step OutEnd 'Phone Number = ' PhoneNum ; Output the information we sought End ; End of the FileDone step NextStep should not be confused with the Stop command, which causes processing to cease entirely. NextStep is also different from Done, which skips the rest of the script and then (if used in the Main step) proceeds to process the next record from the input file. The Done command can, however, be used within a conditional step block (such as FileInit) to skip the rest of that step; in such case it will behave the same way as NextStep.